package xrz.cuanbao.activity;

import java.io.Serializable;

import org.simple.eventbus.EventBus;
import org.xutils.common.util.LogUtil;
import org.xutils.view.annotation.ViewInject;

import xrz.cuanbao.R;
import xrz.cuanbao.modular.reg_login.Act_Login;
import xrz.cuanbao.utils.UserUtils;
import xrz.xuanbao.modular.message.Act_Message;
import xrz.xuanbao.modular.message.Act_MessageFrist;
import xrz.xuanbao.modular.mine.Act_Mine;
import xrz.xuanbao.modular.paipaibao.Act_PaipaiBao;
import xrz.xuanbao.modular.shendaodao.Act_ShenDaodao;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;

import com.xrz.sxm.base.activity.Act_HideTop;

@SuppressWarnings("deprecation")
public class Act_TabMain extends Act_HideTop implements
		OnCheckedChangeListener, Serializable {

	@ViewInject(R.id.tabhost)
	private TabHost m_TabHost;
	@ViewInject(R.id.radiogroup)
	private RadioGroup m_RadioGroup;
	@ViewInject(R.id.rb_1)
	private RadioButton m_renrenRadioButton;

	private LocalActivityManager m_ActivityManager;

	@Override
	protected int setContentView() {
		return R.layout.layout_tabmain;
	}

	@Override
	protected void initData() {
		m_RadioGroup.setOnCheckedChangeListener(this);
		m_ActivityManager = new LocalActivityManager(this, true);
		m_ActivityManager.dispatchCreate(savedInstanceState);
		initTabHost();
	}

	@Override
	protected void onResume() {
		super.onResume();
		m_ActivityManager.dispatchResume();
	}

	private void initTabHost() {
		m_TabHost.setup();
		m_TabHost.setup(m_ActivityManager);
		Intent intent = new Intent(this, Act_RenRenCuan.class);
		m_TabHost.addTab(m_TabHost.newTabSpec("1").setIndicator("1")
				.setContent(intent));
		intent = new Intent(this, Act_PaipaiBao.class);
		m_TabHost.addTab(m_TabHost.newTabSpec("2").setIndicator("2")
				.setContent(intent));
		intent = new Intent(this, Act_ShenDaodao.class);
		m_TabHost.addTab(m_TabHost.newTabSpec("3").setIndicator("3")
				.setContent(intent));
		intent = new Intent(this, Act_MessageFrist.class);
		m_TabHost.addTab(m_TabHost.newTabSpec("3").setIndicator("3")
				.setContent(intent));
		intent = new Intent(this, Act_Mine.class);
		m_TabHost.addTab(m_TabHost.newTabSpec("3").setIndicator("3")
				.setContent(intent));
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		switch (checkedId) {
		case R.id.rb_1:
			m_TabHost.setCurrentTab(0);
			break;
		case R.id.rb_2:
			m_TabHost.setCurrentTab(1);
			break;
		case R.id.rb_3:
			if(UserUtils.checkLogin(m_ctx)){
				m_TabHost.setCurrentTab(2);
			}else{
				m_TabHost.setCurrentTab(1);
				m_renrenRadioButton.setChecked(true);
				skip(Act_Login.class);
			}
			break;
		case R.id.rb_4:
			if(UserUtils.checkLogin(m_ctx)){
				m_TabHost.setCurrentTab(3);
			}else{
				m_TabHost.setCurrentTab(1);
				m_renrenRadioButton.setChecked(true);
				skip(Act_Login.class);
			}
			break;
		case R.id.rb_5:
			if(UserUtils.checkLogin(m_ctx)){
				m_TabHost.setCurrentTab(4);
			}else{
				m_TabHost.setCurrentTab(1);
				m_renrenRadioButton.setChecked(true);
				skip(Act_Login.class);
			}
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		LogUtil.e("requestCode=" + requestCode + "    resultCode="
				+ resultCode);
		onActivityResultData event = new onActivityResultData();
		event.requestCode = requestCode;
		event.resultCode = resultCode;
		event.data = data;
		EventBus.getDefault().post(event, "activityResultData");
	}

	public class onActivityResultData implements Serializable {
		public int requestCode;
		public int resultCode;
		public Intent data;
	}
	@Override
	protected boolean keyDownFinish() {
		return true;
	}
}
